//index.js
//获取应用实例
const app = getApp()
import { addreceiver } from '../../../http/api'

Page({
  data: {
    area:'',
    receiverName: '',
    receiverPhone: '',
    detailAddress: '',
    provinceName:'',
    cityName:'',
    districtName:''
  },
  //选择地址返回处理
  onShow: function () {
    let provinceName = wx.getStorageSync('locationState').provinceName;
    let cityName = wx.getStorageSync('locationState').cityName;
    let districtName = wx.getStorageSync('locationState').districtName;
    this.setData({
      provinceName,
      cityName,
      districtName,
      area:`${provinceName} ${cityName} ${districtName}`
    });
  },
  //联系人
  handleLinkMan: function (e) {
    this.setData({
      receiverName: e.detail.value
    })
  },
  //手机号
  handlePhone: function (e) {
    this.setData({
      receiverPhone: e.detail.value
    })
  },
  //门牌号
  handleStreet: function (e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },

  //创建收货地址
  addUserAddress: function () {
    var receiverPhone = this.data.receiverPhone;
    if (!this.data.receiverName) {
      wx.showToast({ title: '请填写收货人！', duration: 1000, icon: 'none' });
      return;
    }
    if (!this.data.receiverPhone) {
      wx.showToast({ title: '请填写手机号！', duration: 1000, icon: 'none' });
      return;
    }
    if(!(/^1[3456789]\d{9}$/.test(receiverPhone))){ 
      wx.showToast({ title: '手机号码有误，请重填！', duration: 1000, icon: 'none' });
        return; 
    }
    if (!this.data.detailAddress) {
      wx.showToast({ title: '请填写详细地址！', duration: 1000, icon: 'none' });
      return;
    }
    let obj = {
      receiverName: this.data.receiverName,
      receiverPhone: this.data.receiverPhone,
      detailAddress: this.data.detailAddress,
      provinceName: this.data.provinceName,
      cityName: this.data.cityName,
      districtName: this.data.districtName,
      provinceId: 440000000000,
      cityId: 440300000000,
      districtId: 440306000000,
      receiverSelected: 0
    };
    addreceiver(obj).then(res => {
      if (res.code == '200') {
        wx.navigateBack({
          delta: 1
        })
      }
    });
  }
})
